//
//  WPTradeListViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/5.
//  Copyright © 2016年 HuaYiSoftware. All rights reserved.
//

#import "WPTradeListViewController.h"
#import "WPRightTransModel.h"
#import "WPTradeListTableViewCell.h"
#import "WPTradeOrderViewController.h"

static NSString *const kUITableViewCellIdentifier = @"cellIdentifier";
static NSInteger sortBtnTag = 400;
@interface WPTradeListViewController ()<UITableViewDataSource, UITableViewDelegate, WPTradeListTableViewCellDelegate>
@property (nonatomic, strong) UIView * headView;/**< 头部视图 */
@property (nonatomic, strong) UIImageView * introImageView;/**< 海报 */
@property (nonatomic, strong) UIView * ticketView;/**< 票视图 */
@property (nonatomic, strong) UITextField * ticketCountTextField;/**< 成功交易张数 */
@property (nonatomic, strong) UILabel * allTicketCountLabel;/**< 总票数 */
@property (nonatomic, strong) UITextField * ticketIntroTextField;/**< 票数说明 */
@property (nonatomic, strong) UITextField * joinCountTextField;/**< 参与人数 */
@property (nonatomic, strong) UITextField * daysTextField;/**< 剩余天数 */
@property (nonatomic, strong) UITextField * joinIntroTextField;/**< 参与人数说明 */
@property (nonatomic, strong) UITextField * leftDaysTextField;/**< 剩余天数说明 */

@property (nonatomic, strong) UIView * progressTitle;/**< 进展图标题 */
@property (nonatomic, strong) UIView * priceTitle;/**< 价格走势标题 */
@property (nonatomic, strong) UIView * progressChart;/**< 进展图 */
@property (nonatomic, strong) UIView * trendChart;/**< 价格走势图 */
@property (nonatomic, strong) UIView * sortBtnView;/**< 排序按钮视图 */
@property (nonatomic, strong) UITableView * tradeTableView;/**< 交易期表格视图 */
@property (nonatomic, strong) NSMutableArray * tradeListArr;/**< 交易期数据源 */
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
- (void)refreshData;/**< 刷新数据 */
@end

@implementation WPTradeListViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeDataSource {
    self.tradeListArr = [NSMutableArray array];
    WPRightTransModel * model = [WPRightTransModel new];
    model.name = @"12312";
    model.headImage = @"banner1";
    model.type = @"单人票";
    model.position =  @"A区1排25号";
    model.price = @"¥1140";
    [self.tradeListArr addObject:model];
}
- (void)initializeUserInterface {
    [self.view addSubview:self.tradeTableView];
}
#pragma mark - responds events
- (void)respondsToProgressSpreadBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (void)respondsToPriceSpreadBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (void)respondsToSortBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
}
#pragma mark - 刷新数据
- (void)refreshData {
    
}
#pragma mark - 协议UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _tradeListArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WPTradeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    if (!cell) {
        cell = [[WPTradeListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kUITableViewCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    [cell refreshDataWithModel:_tradeListArr[indexPath.section]];
    return cell;
}
#pragma mark - UITableViewDelegate

//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 128*DHFlexibleHorizontalMutiplier();
}
#pragma mark - private protocol
- (void)respondsToPositionBtn {
    
}
- (void)respondsToBuyBtn {
    WPTradeOrderViewController * vc = [WPTradeOrderViewController new];
    [self pushToNextController:vc withVCTitle:@"交易订单"];
}
#pragma mark - private methods
- (NSMutableAttributedString *)configAttributtedStringWithString:(NSString *)str {
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@张", str]];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(242, 106, 89, 1) range:NSMakeRange(0, str.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(141, 141, 141, 1) range:NSMakeRange(str.length, 1)];
    return attributedStr;
}
- (void)configSortBtn {
    NSArray * btnImages = @[@"A区.png", @"时间降序.png", @"类型.png", @"价格.png"];
    NSArray * btnSeletctImages = @[@"A区.png", @"时间升序.png", @"类型.png", @"价格升序.png"];
    for (int i = 0; i < btnImages.count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.bounds = CGRectMake(0, 0, 30*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier());
        btn.center = CGPointMake((50+i%4*70)*DHFlexibleHorizontalMutiplier(), CGRectGetMidY(self.sortBtnView.bounds));
        [btn setImage:IMAGE_NAME(btnImages[i]) forState:UIControlStateNormal];
        [btn setImage:IMAGE_NAME(btnSeletctImages[i]) forState:UIControlStateSelected];
        btn.tag = sortBtnTag + i;
        [btn addTarget:self action:@selector(respondsToSortBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_sortBtnView addSubview:btn];
    }
}
#pragma makr - getter
- (UIView *)headView {
    if (!_headView) {
        _headView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, navBar_Fheight, SCREEN_SIZE.width, 295*DHFlexibleHorizontalMutiplier())];
            [view addSubview:self.introImageView];
            [view addSubview:self.ticketView];
            UIView * sepa = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.ticketView.frame), SCREEN_SIZE.width, 3*DHFlexibleHorizontalMutiplier())];
            sepa.backgroundColor = COLOR_RGB(241, 241, 241, 1);
            [view addSubview:sepa];
            [view addSubview:self.progressTitle];
            [view addSubview:self.priceTitle];
            [view addSubview:self.sortBtnView];
            [self configSortBtn];
            view;
        });
    }
    return _headView;
}
- (UIImageView *)introImageView {
    if (!_introImageView) {
        _introImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 144*DHFlexibleHorizontalMutiplier())];
            view.image = IMAGE_NAME(@"banner1");
            view;
        });
    }
    return _introImageView;
}
- (UIView *)ticketView {
    if (!_ticketView) {
        _ticketView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.introImageView.frame), SCREEN_SIZE.width, 44*DHFlexibleHorizontalMutiplier())];
            [view addSubview:self.ticketCountTextField];
            [view addSubview:self.allTicketCountLabel];
            [view addSubview:self.ticketIntroTextField];
            
            [view addSubview:self.joinCountTextField];
            [view addSubview:self.joinIntroTextField];
            
            [view addSubview:self.daysTextField];
            [view addSubview:self.leftDaysTextField];
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_SIZE.width/3.0, 0, 1, CGRectGetHeight(view.frame))];
            line.backgroundColor = COLOR_RGB(241, 241, 241, 1);
            [view addSubview:line];
            UIView * lineRight = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_SIZE.width/3.0*2, 0, 1, CGRectGetHeight(view.frame))];
            lineRight.backgroundColor = COLOR_RGB(241, 241, 241, 1);
            [view addSubview:lineRight];
            view;
        });
    }
    return _ticketView;
}
- (UITextField *)ticketCountTextField {
    if (!_ticketCountTextField) {
        _ticketCountTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            text.rightViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 15, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:11*DHFlexibleHorizontalMutiplier()];
            lab.text = @"张/";
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            text.rightView = lab;
            text.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(234, 27, 47, 1);
            text.textAlignment = NSTextAlignmentRight;
            text.text = _model.ticketCount;
            text.userInteractionEnabled = NO;
            text;
        });
    }
    return _ticketCountTextField;
}
- (UILabel *)allTicketCountLabel {
    if (!_allTicketCountLabel) {
        _allTicketCountLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(60*DHFlexibleHorizontalMutiplier(), 0, 60*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.userInteractionEnabled = NO;
            lab.attributedText = [self configAttributtedStringWithString:_model.allTicketCount];
            lab;
        });
    }
    return _allTicketCountLabel;
}
- (UITextField *)joinCountTextField {
    if (!_joinCountTextField) {
        _joinCountTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(150*DHFlexibleHorizontalMutiplier(), 0, 60*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            text.rightViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 15, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:11*DHFlexibleHorizontalMutiplier()];
            lab.text = @"人";
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            text.rightView = lab;
            text.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(234, 27, 47, 1);
            text.textAlignment = NSTextAlignmentCenter;
            text.userInteractionEnabled = NO;
            text.text = _model.joinCount;
            text;
        });
    }
    return _joinCountTextField;
}
- (UITextField *)daysTextField {
    if (!_daysTextField) {
        _daysTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(220*DHFlexibleHorizontalMutiplier(),0, 50*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            text.rightViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 15*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:11*DHFlexibleHorizontalMutiplier()];
            lab.text = @"天";
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            text.rightView = lab;
            text.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(234, 27, 47, 1);
            text.textAlignment = NSTextAlignmentRight;
            text.userInteractionEnabled = NO;
            text.text = _model.leftDays;
            text;
        });
    }
    return _daysTextField;
}
- (UITextField *)ticketIntroTextField {
    if (!_ticketIntroTextField) {
        _ticketIntroTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.allTicketCountLabel.frame), 105*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            text.font = [UIFont systemFontOfSize:11*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(141, 141, 141, 1);
            text.textAlignment = NSTextAlignmentRight;
            text.userInteractionEnabled = NO;
            text.text = @"成功交易 / 交易总数";
            text;
        });
    }
    return _ticketIntroTextField;
}
- (UITextField *)joinIntroTextField {
    if (!_joinIntroTextField) {
        _joinIntroTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.joinCountTextField.frame)*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.joinCountTextField.frame), 50*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(141, 141, 141, 1);
            text.userInteractionEnabled = NO;
            text.text = @"参与人数";
            text;
        });
    }
    return _joinIntroTextField;
}
- (UITextField *)leftDaysTextField {
    if (!_leftDaysTextField) {
        _leftDaysTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(210*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.daysTextField.frame), 70*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(141, 141, 141, 1);
            text.textAlignment = NSTextAlignmentRight;
            text.userInteractionEnabled = NO;
            text.text = @"剩余时间";
            text;
        });
    }
    return _leftDaysTextField;
}
- (UIView *)progressTitle {
    if (!_progressTitle) {
        _progressTitle = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.ticketView.frame)+5*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view.bounds)-1, SCREEN_SIZE.width, 1)];
            line.backgroundColor = COLOR_RGB(241, 241, 241, 1);
            [view addSubview:line];
            UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(20*DHFlexibleHorizontalMutiplier(), 7*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier())];
            image.image = IMAGE_NAME(@"jz");
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(40*DHFlexibleHorizontalMutiplier(), 0, 200, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"项目进展";
            UIButton * spreadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            spreadBtn.frame = CGRectMake(280*DHFlexibleHorizontalMutiplier(), 12*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 6*DHFlexibleHorizontalMutiplier());
            [spreadBtn setImage:IMAGE_NAME(@"xialajiantou") forState:UIControlStateNormal];
            [spreadBtn setImage:IMAGE_NAME(@"iconfont-shouqi") forState:UIControlStateSelected];
            [spreadBtn addTarget:self action:@selector(respondsToProgressSpreadBtn:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:image];
            [view addSubview:lab];
            [view addSubview:spreadBtn];
            view;
        });
    }
    return _progressTitle;
}
- (UIView *)priceTitle {
    if (!_priceTitle) {
        _priceTitle = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.progressTitle.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view.bounds)-1, SCREEN_SIZE.width, 1)];
            line.backgroundColor = COLOR_RGB(241, 241, 241, 1);
            [view addSubview:line];
            UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(20*DHFlexibleHorizontalMutiplier(), 7*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier())];
            image.image = IMAGE_NAME(@"jz");
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(40*DHFlexibleHorizontalMutiplier(), 0, 200, 30*DHFlexibleHorizontalMutiplier())];
            lab.text = @"价格走势";
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            UIButton * spreadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            spreadBtn.frame = CGRectMake(280*DHFlexibleHorizontalMutiplier(), 12*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 6*DHFlexibleHorizontalMutiplier());
            [spreadBtn setImage:IMAGE_NAME(@"xialajiantou") forState:UIControlStateNormal];
            [spreadBtn setImage:IMAGE_NAME(@"iconfont-shouqi") forState:UIControlStateSelected];
            [spreadBtn addTarget:self action:@selector(respondsToProgressSpreadBtn:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:image];
            [view addSubview:lab];
            [view addSubview:spreadBtn];
            view;
        });
    }
    return _priceTitle;
}
- (UIView *)sortBtnView {
    if (!_sortBtnView) {
        _sortBtnView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.priceTitle.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = COLOR_RGB(241, 241, 241, 1);
            view;
        });
    }
    return _sortBtnView;
}
- (UITableView *)tradeTableView {
    if (!_tradeTableView) {
        _tradeTableView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, navBar_Fheight, SCREEN_SIZE.width, SCREEN_SIZE.height - navBar_Fheight) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableHeaderView = self.headView;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.bounces = NO;
            tableview.separatorColor = COLOR_RGB(211, 211, 211, 1);
            tableview;
        });
    }
    return _tradeTableView;
}
@end
